﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для ucSelectVehicle.xaml
    /// </summary>
    public partial class ucSelectVehicle : UserControl
    {



        public vehicle vehicle
        {
            get { return (vehicle)GetValue(vehicleProperty); }
            set { SetValue(vehicleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for vehicle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty vehicleProperty =
            DependencyProperty.Register("vehicle", typeof(vehicle), typeof(ucSelectVehicle), new UIPropertyMetadata(null, new PropertyChangedCallback(propVehicle)));

        private static void propVehicle(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucSelectVehicle item = d as ucSelectVehicle;
            if (null == item) return;
            vehicle newv = e.NewValue as vehicle;
            if (null != newv)
            {
                item.textBox1.Text = string.Format("{0}({1})",newv.gar_number.Trim(), newv.gos_number.Trim());
            }
        }



        public int? VehicleID
        {
            get { return (int?)GetValue(VehicleIDProperty); }
            set { SetValue(VehicleIDProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VehicleID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VehicleIDProperty =
            DependencyProperty.Register("VehicleID", typeof(int?), typeof(ucSelectVehicle), new UIPropertyMetadata(null, new PropertyChangedCallback(propVehicleID)));
        public static void propVehicleID(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucSelectVehicle item = d as ucSelectVehicle;
            if (null == item) return;
            int? val = (int?)e.NewValue;
            if (item.ContextDB == null)
            {
                item.ContextDB = new routelistEntities();
            }
            item.vehicle = (from o in item.ContextDB.vehicle where o.id == val select o).FirstOrDefault();
            item.textBox1.Text = item.vehicle == null ? "<не выбран>" : string.Format("{0}({1})", item.vehicle.gar_number.Trim(), item.vehicle.gos_number.Trim());
        }
        

        public routelistEntities ContextDB
        {
            get { 
                return (routelistEntities)GetValue(ContextDBProperty);
            }
            set {
                SetValue(ContextDBProperty, value); 
            }
        }

        // Using a DependencyProperty as the backing store for ContextDB.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContextDBProperty =
            DependencyProperty.Register("ContextDB", typeof(routelistEntities), typeof(ucSelectVehicle), new UIPropertyMetadata(null));

        

        public ucSelectVehicle()
        {
            InitializeComponent();
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
            ContextDB = new routelistEntities();
            textBox1.Text = "<не указан>";
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void buttonSelectFromReestr_Click(object sender, RoutedEventArgs e)
        {
            if (null == ContextDB) return;
            wVehicle wnd = new wVehicle();
            //wnd.ContextDB = ContextDB;
            wnd.VehicleID = vehicle == null ? 0 : vehicle.id;
            if (wnd.ShowDialog() == true)
            {
                vehicle = wnd.vehicle;
                VehicleID = vehicle == null ? 0 : wnd.vehicle.id;
                //if (null != vehicle)
                //{
                //    textBox1.Text = string.Format("{0}({1})",vehicle.gar_number.Trim(),vehicle.gos_number.Trim());
                //}
            }
        }

        private void buttonClear_Click(object sender, RoutedEventArgs e)
        {
            VehicleID = null;
        }
    }

    [ValueConversion(typeof(int), typeof(String))]
    public class VehicleIDConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (null == value) return "<не выбран>";
            int item_i = (int)value;
            routelistEntities db = new routelistEntities();
            vehicle item = (from o in db.vehicle where o.id == item_i select o).FirstOrDefault();
            if (null == item) return "<не выбран>";
            string result = string.Format("({0}) {1}", item.gar_number.Trim(), item.gos_number.Trim());
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
